package com.yj.jason.jsondemo;

import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.FloatMath;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yj.jason.baselibrary.utils.StatusBarUtil;
import com.yj.jason.moudlelibrary.view.StepView;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.Set;

public class TagActivity extends AppCompatActivity{

    //放入流式布局标签中的内容
    private String[] mVals = new String[]
            {"有信用卡", "有微粒贷有微粒贷有微粒贷有微粒贷有微粒贷有微粒贷有微粒贷有微粒贷有微粒贷有微粒贷有微粒贷有微粒贷有微粒贷有微粒贷有微粒贷有微粒贷有微粒贷有微粒贷有微粒贷有微粒贷有微粒贷有微粒贷有微粒贷有微粒贷", "我有房", "我有车", "有社保", "有公积金",
                    "有人寿保险", "工资银行卡转账", "啥都没有"};
    private TagFlowLayout mFlowLayout;

    private StepView step_view;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tag);

        StatusBarUtil.setStatusBarColor(this, Color.GREEN);

        mFlowLayout=(TagFlowLayout)findViewById(R.id.id_flowlayout);
        step_view=(StepView)findViewById(R.id.step_view);

        step_view.setProgress(0.8f);
      //  image=(ImageView)findViewById(R.id.image);
//获取布局填充器,一会将tv.xml文件填充到标签内.
        final LayoutInflater mInflater = LayoutInflater.from(this);
        mFlowLayout.setAdapter(new TagAdapter<String>(mVals)
        {

            @Override
            public View getView(com.zhy.view.flowlayout.FlowLayout parent, int position, String s)
            {
//                将tv.xml文件填充到标签内.
                TextView tv = (TextView) mInflater.inflate(R.layout.tv,
                        mFlowLayout, false);
//               为标签设置对应的内容
                tv.setText(s);
                return tv;
            }
            //             为标签设置预点击内容(就是一开始就处于点击状态的标签)
            @Override
            public boolean setSelected(int position, String s)
            {
                return s.equals("Android");
            }
        });
//          为点击标签设置点击事件.
        mFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener()
        {
            @Override
            public boolean onTagClick(View view, int position, com.zhy.view.flowlayout.FlowLayout parent)
            {
                Toast.makeText(TagActivity.this, mVals[position], Toast.LENGTH_SHORT).show();
                //view.setVisibility(View.GONE);
                return true;
            }
        });

//          展示哪些标签处于选中状态,这个很重要我们设置标签可点击就是为了把用户选中状态的标签中的数据上传.
        mFlowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener()
        {
            @Override
            public void onSelected(Set<Integer> selectPosSet)
            {
                setTitle("choose:" + selectPosSet.toString());
            }
        });

       // image.setOnTouchListener(new TouchListener());


    }


  /*  private final class TouchListener implements View.OnTouchListener {

        *//** 记录是拖拉照片模式还是放大缩小照片模式 *//*
        private int mode = 0;// 初始状态
        *//** 拖拉照片模式 *//*
        private static final int MODE_DRAG = 1;
        *//** 放大缩小照片模式 *//*
        private static final int MODE_ZOOM = 2;

        *//** 用于记录开始时候的坐标位置 *//*
        private PointF startPoint = new PointF();
        *//** 用于记录拖拉图片移动的坐标位置 *//*
        private Matrix matrix = new Matrix();
        *//** 用于记录图片要进行拖拉时候的坐标位置 *//*
        private Matrix currentMatrix = new Matrix();

        *//** 两个手指的开始距离 *//*
        private float startDis;
        *//** 两个手指的中间点 *//*
        private PointF midPoint;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            *//** 通过与运算保留最后八位 MotionEvent.ACTION_MASK = 255 *//*
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                // 手指压下屏幕
                case MotionEvent.ACTION_DOWN:
                    mode = MODE_DRAG;
                    // 记录ImageView当前的移动位置
                    currentMatrix.set(image.getImageMatrix());
                    startPoint.set(event.getX(), event.getY());
                    break;
                // 手指在屏幕上移动，改事件会被不断触发
                case MotionEvent.ACTION_MOVE:
                    // 拖拉图片
                    if (mode == MODE_DRAG) {
                        float dx = event.getX() - startPoint.x; // 得到x轴的移动距离
                        float dy = event.getY() - startPoint.y; // 得到x轴的移动距离
                        // 在没有移动之前的位置上进行移动
                        matrix.set(currentMatrix);
                        matrix.postTranslate(dx, dy);
                    }
                    // 放大缩小图片
                    else if (mode == MODE_ZOOM) {
                        float endDis = distance(event);// 结束距离
                        if (endDis > 10f) { // 两个手指并拢在一起的时候像素大于10
                            float scale = endDis / startDis;// 得到缩放倍数
                            matrix.set(currentMatrix);
                            matrix.postScale(scale, scale,midPoint.x,midPoint.y);
                        }
                    }
                    break;
                // 手指离开屏幕
                case MotionEvent.ACTION_UP:
                    // 当触点离开屏幕，但是屏幕上还有触点(手指)
                case MotionEvent.ACTION_POINTER_UP:
                    mode = 0;
                    break;
                // 当屏幕上已经有触点(手指)，再有一个触点压下屏幕
                case MotionEvent.ACTION_POINTER_DOWN:
                    mode = MODE_ZOOM;
                    *//** 计算两个手指间的距离 *//*
                    startDis = distance(event);
                    *//** 计算两个手指间的中间点 *//*
                    if (startDis > 10f) { // 两个手指并拢在一起的时候像素大于10
                        midPoint = mid(event);
                        //记录当前ImageView的缩放倍数
                        currentMatrix.set(image.getImageMatrix());
                    }
                    break;
            }
            image.setImageMatrix(matrix);
            return true;
        }

        *//** 计算两个手指间的距离 *//*
        private float distance(MotionEvent event) {
            float dx = event.getX(1) - event.getX(0);
            float dy = event.getY(1) - event.getY(0);
            *//** 使用勾股定理返回两点之间的距离 *//*
            return (float) Math.sqrt(dx * dx + dy * dy);
        }

        *//** 计算两个手指间的中间点 *//*
        private PointF mid(MotionEvent event) {
            float midX = (event.getX(1) + event.getX(0)) / 2;
            float midY = (event.getY(1) + event.getY(0)) / 2;
            return new PointF(midX, midY);
        }*/


}
